﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using StoragePeriod.Data;

namespace StoragePeriod.Web.Services
{
    public class CommonServices : IAjaxService
    {
        public AjaxResult Excute(HttpContext httpContext)
        {
            string FunType = httpContext.Request["FunType"];
            AjaxResult result = null;
            switch (FunType.ToLower())
            {
                case "rawtype":
                    result = GetRawType(httpContext);
                    break;
                case "notepad":
                    result = LoadNotePad(httpContext);
                    break;
                case "normtype":
                    result = GetNormType(httpContext);
                    break;
            }
            return result;
        }

        private string[] weekDays = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        private AjaxResult LoadNotePad(HttpContext context)
        {
            string dataType = context.Request["datatype"];
            string type = context.Request["type"];
            var res = new ArrayList();
            string strdatetime = DateTime.Now.ToString("yyyy年MM月dd日") + "    " + weekDays[(int)DateTime.Now.DayOfWeek] + "    " + DateTime.Now.ToString("HH:mm");
            
            using (StoragePeriodDataContext data = new StoragePeriodDataContext())
            {
                switch (dataType)
                {
                    case "simple":

                        PanelsConfig md = data.PanelsConfig.Where("TypeId=@0", Convert.ToInt32(EnumData.PanelsType.公告内容)).OrderBy(p => p.id).FirstOrDefault();
                        string strbody = (md != null ? md.Contents : "");
                        res.Add(new { body = strbody, datetime = strdatetime });

                        break;
                    case "list":
                        var list = new ArrayList();
                        List<PanelsConfig> mdlist = data.PanelsConfig.Where("TypeId=@0", Convert.ToInt32(EnumData.PanelsType.实验室使用状态)).OrderBy(p => p.id).Take(10).ToList();
                        int XH = 1;
                        mdlist.ForEach(p =>
                        {
                            list.Add(new { Number = XH.ToString(), Name = p.title, Start = p.UseState, Project = p.TestItem, ChargeUser = p.chargePerson, Tel = p.Tel });
                            XH++;
                        });
                        res.Add(new { list = list, datetime = strdatetime });
                        break;
                }
            }

            return new AjaxResult(true, res);
        }

        private AjaxResult GetRawType(HttpContext httpContext)
        {
            int ParentId = Convert.ToInt32(httpContext.Request["ParentId"]);
            var res = new ArrayList();
            using (StoragePeriodDataContext dataContext = new StoragePeriodDataContext())
            {
                List<RawMaterialType> TypeList = dataContext.RawMaterialType.Where("ParentId=@0", ParentId).ToList();
                TypeList.ForEach(p =>
                {
                    res.Add(new
                    {
                        Value = p.id,
                        Text = p.TypeName
                    });
                });
            }
            return new AjaxResult(true, res);
        }

        private AjaxResult GetNormType(HttpContext httpContext)
        {
            int One_Norm = Convert.ToInt32(httpContext.Request["ParentId"]);
            var res = new ArrayList();
            using (StoragePeriodDataContext dataContext = new StoragePeriodDataContext())
            {
                List<RawMaterialType> TypeList = dataContext.RawMaterialType.Where("ParentId=@0", One_Norm).ToList();
                TypeList.ForEach(p =>
                {
                    res.Add(new
                    {
                        Value = p.id,
                        Text = p.TypeName
                    });
                });
            }
            return new AjaxResult(true, res);
        }
    }
}